﻿using Max.PH4_2101A.ShoppingAdmin.FuXi.API.SecondWeekExam.Domain;
using Max.PH4_2101A.ShoppingAdmin.FuXi.API.SecondWeekExam.IRespository;
using Max.PH4_2101A.ShoppingAdmin.FuXi.API.SecondWeekExam.IService;

namespace Max.PH4_2101A.ShoppingAdmin.FuXi.API.SecondWeekExam.Service
{
    public class ProductService : IProductService
    {
        IProductRespository _respository;
        IBrandRespository _brandRespository;

        public ProductService(IProductRespository respository, IBrandRespository brandRespository)
        {
            _respository = respository;
            _brandRespository = brandRespository;
        }

        public int Insert(ProductModel model)
        {
            int result = _respository.Insert(model);
            return result;
        }

        public IQueryable<dynamic> Query(string name, string brandId, string date1, string date2)
        {
            //var data = _respository.Query().Where(x => x.Name.Contains(name ?? ""));

            var q = from a in _respository.Query()
                    join b in _brandRespository.Query() on a.BrandId equals b.Id
                    where a.Name.Contains(name??"")                    
                    select new { 
                        a.Id,
                        a.Name,
                        a.BrandId,
                        BrandName = b.Name
                    };

            if (brandId != "-1")
            {
                q = q.Where(x => x.BrandId == Convert.ToInt32(brandId));
            }
            return q;
        }
    }
}
